<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

$GLOBALS['TL_DCA']['tl_project'] = array
( 
'config' => array
	(
		'dataContainer'               => 'Table',
		'ctable'					=> array('tl_project_object'),
		'switchToEdit'                => true
	), 

    // List
    'list' => array
    (
        'sorting' => array
        (
            'mode'                    => 1,
            'fields'                  => array('title'),
            'panelLayout'             => 'search,limit',
        	'flag'                    => 1,
        	'child_record_callback'   => array('ProjectClass', 'listObject')
        ),
        'label' => array
        (
			'fields'                  => array('singleSRC','title'),
			'format'                  => '<img src="%s" width="150" /> %s',
        ),
        'global_operations' => array
		(
			'all' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['MSC']['all'],
				'href'                => 'act=select',
				'class'               => 'header_edit_all',
				'attributes'          => 'onclick="Backend.getScrollOffset();" accesskey="e"'
			)
		),
        'operations' => array
		(
			'edit' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['MSC']['edit'],
				'href'                => 'table=tl_project_object',
				'icon'                => 'edit.gif',
			),
			'copy' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['MSC']['copy'],
				'href'                => 'act=copy',
				'icon'                => 'copy.gif',
			),
			'delete' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_project_object']['delete'],
				'href'                => 'act=delete',
				'icon'                => 'delete.gif',
				'attributes'          => 'onclick="if (!confirm(\'' . $GLOBALS['TL_LANG']['MSC']['deleteConfirm'] . '\')) return false; Backend.getScrollOffset();"',
			),
			'toggle' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_news']['toggle'],
				'icon'                => 'visible.gif',
				'attributes'          => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"',
				'button_callback'     => array('ProjectClass', 'toggleIcon')
			),
			'show' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_news']['show'],
				'href'                => 'act=show',
				'icon'                => 'show.gif'
			)
		)
    ),

    // Palettes
	'palettes' => array
	(
		'__selector__'                => array('addImage'),
		'default'                     => '{title_legend},title;type;status;{image_legend},addImage;{thongtin_legend},thongtin;{vitri_legend},vitri;{dientich_legend},dientich;{matbang_legend},matbang;{tiendo_legend},tiendo;{tintuc_legend},tintuc;{hinhanh_legend},hinhanh;{video_legend},video;{lienhe_legend},lienhe;jumpTo;published,start,stop'
	),
    
    // Subpalettes
	'subpalettes' => array
	(
		'addImage'                    => 'singleSRC',
	),
    // Fields
    'fields' => array
    (
        'title' => array
        (
            'label'                   => &$GLOBALS['TL_LANG']['tl_project']['title'],
            'inputType'               => 'text',
            'search'                  => true,
            'eval'                    => array('mandatory'=>true, 'maxlength'=>255, 'tl_class'=>'w50')
        ),
        'type'  => array
        (
            'label'                   => &$GLOBALS['TL_LANG']['tl_project']['type'],
            'inputType'               => 'select',
            'options_callback'        => array('ProjectClass', 'getType'),
            'search'                  => true,
            'sorting'                 => true,
            'filter'                  => true,
            'exclude'                 => true,
            'flag'                    => 11,
            'eval'                    => array('mandatory'=>true, 'tl_class'=>'w50')
        ),
        'status'  => array
        (
            'label'                   => &$GLOBALS['TL_LANG']['tl_project']['status'],
            'inputType'               => 'select',
            'options_callback'        => array('ProjectClass', 'getStatus'),
            'search'                  => true,
            'sorting'                 => true,
            'filter'                  => true,
            'exclude'                 => true,
            'flag'                    => 11,
            'eval'                    => array('mandatory'=>true, 'tl_class'=>'w50')
        ),
		'thongtin' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['thongtin'],
			'exclude'                 => true,
			'inputType'               => 'textarea',
			'eval'                    =>  array('rte'=>'tinyMCE', 'tl_class'=>'clr')
		),
		'vitri' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['vitri'],
			'exclude'                 => true,
			'search'                  => true,
			'inputType'               => 'textarea',
			'eval'                    =>  array('rte'=>'tinyMCE', 'tl_class'=>'clr')
		),
		'dientich' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['dientich'],
			'exclude'                 => true,
			'search'                  => true,
			'inputType'               => 'textarea',
			'eval'                    =>  array('rte'=>'tinyMCE', 'tl_class'=>'clr')
		),
		'matbang' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['matbang'],
			'exclude'                 => true,
			'search'                  => true,
			'inputType'               => 'textarea',
			'eval'                    =>  array('rte'=>'tinyMCE', 'tl_class'=>'clr')
		),
		'tiendo' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['tiendo'],
			'exclude'                 => true,
			'search'                  => true,
			'inputType'               => 'textarea',
			'eval'                    =>  array('rte'=>'tinyMCE', 'tl_class'=>'clr')
		),
		'tintuc' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['tintuc'],
			'exclude'                 => true,
			'search'                  => true,
			'inputType'               => 'textarea',
			'eval'                    =>  array('rte'=>'tinyMCE', 'tl_class'=>'clr')
		),
		'hinhanh' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['hinhanh'],
			'search'                  => true,
			'inputType'               => 'textarea',
			'eval'                    =>  array('rte'=>'tinyMCE', 'tl_class'=>'clr')
		),
		'video' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['video'],
			'search'                  => true,
			'inputType'               => 'textarea',
			'eval'                    =>  array('rte'=>'tinyMCE', 'tl_class'=>'clr')
		),
		'lienhe' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['lienhe'],
			'search'                  => true,
			'inputType'               => 'textarea',
			'eval'                    =>  array('rte'=>'tinyMCE', 'tl_class'=>'clr')
		),
		'jumpTo' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['jumpTo'],
			'exclude'                 => true,
			'inputType'               => 'pageTree',
			'eval'                    => array('fieldType'=>'radio')
		),
		'addImage' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['addImage'],
			'exclude'                 => true,
			'inputType'               => 'checkbox',
			'eval'                    => array('submitOnChange'=>true)
		),
		'singleSRC' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['singleSRC'],
			'exclude'                 => true,
			'inputType'               => 'fileTree',
			'eval'                    => array('fieldType'=>'radio', 'files'=>true, 'filesOnly'=>true, 'mandatory'=>true)
		),
		'published' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['published'],
			'exclude'                 => true,
			'filter'                  => true,
			'flag'                    => 1,
			'inputType'               => 'checkbox',
			'eval'                    => array('doNotCopy'=>true)
		),
		'start' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['start'],
			'exclude'                 => true,
			'inputType'               => 'text',
			'eval'                    => array('rgxp'=>'datim', 'datepicker'=>true, 'tl_class'=>'w50 wizard')
		),
		'stop' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_project']['stop'],
			'exclude'                 => true,
			'inputType'               => 'text',
			'eval'                    => array('rgxp'=>'datim', 'datepicker'=>true, 'tl_class'=>'w50 wizard')
		)
    )
);

class ProjectClass extends Backend
{
    public function getType($dc)
    {
        $objType = $this->Database->execute("SELECT id, name FROM tl_project_type");

        if ($objType->numRows < 1)
        {
            return array();
        }

        $arrType = array();

        // Back end
        if (TL_MODE == 'BE')
        {
            while ($objType->next())
            {
                $arrType[$objType->id] = $objType->name;
            }

            return $arrType;
        }
        var_dump($arrType);
        return $arrType;
    }
    public function getStatus($dc)
    {
    	$arrStatus = array();
    	$arrStatus[1] = 'Dự án đang triển khai';
    	$arrStatus[2] = 'Dự án sắp triển khai';
    	$arrStatus[3] = 'Dự án đã triển khai';
    	$arrStatus[3] = 'Dự án khác';
    	return $arrStatus;
    }
    public function listObject($arrRow)
	{
		
		return "<img src='".$arrRow['singleSRC']."' width='150' /> ".$arrRow['title']."\n";
	}
    	/**
	 * Return the "toggle visibility" button
	 * @param array
	 * @param string
	 * @param string
	 * @param string
	 * @param string
	 * @param string
	 * @return string
	 */
	public function toggleIcon($row, $href, $label, $title, $icon, $attributes)
	{
		if (strlen($this->Input->get('tid')))
		{
			$this->toggleVisibility($this->Input->get('tid'), ($this->Input->get('state') == 1));
			$this->redirect($this->getReferer());
		}

		$href .= '&amp;tid='.$row['id'].'&amp;state='.($row['published'] ? '' : 1);

		if (!$row['published'])
		{
			$icon = 'invisible.gif';
		}		

		return '<a href="'.$this->addToUrl($href).'" title="'.specialchars($title).'"'.$attributes.'>'.$this->generateImage($icon, $label).'</a> ';
	}
	public function toggleVisibility($intId, $blnVisible)
	{
		$this->createInitialVersion('tl_project', $intId);
	
		// Update the database
		$this->Database->prepare("UPDATE tl_project SET tstamp=". time() .", published='" . ($blnVisible ? 1 : '') . "' WHERE id=?")
					   ->execute($intId);

		$this->createNewVersion('tl_project', $intId);

	}
}

?>
